<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品编号" prop="productId">
        <el-input
          v-model="queryParams.productId"
          placeholder="请输入商品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户编号" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物流单号" prop="sendId">
        <el-input
          v-model="queryParams.sendId"
          placeholder="请输入物流单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable>
          <el-option
            v-for="dict in dict.type.shop_order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['cart:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['cart:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['cart:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['cart:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="orderId" />
      <el-table-column label="商铺编号" align="center" prop="shopsId" />
      <el-table-column label="商品编号" align="center" prop="productId" />
      <el-table-column label="用户编号" align="center" prop="userId" />
      <el-table-column label="优惠券编号" align="center" prop="couponId" />
      <el-table-column label="商品数量" align="center" prop="productCount" />
      <el-table-column label="支付方式" align="center" prop="payType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.shop_order_pay_type" :value="scope.row.payType"/>
        </template>
      </el-table-column>
      <el-table-column label="配送方式" align="center" prop="sendType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.shop_order_send_type" :value="scope.row.sendType"/>
        </template>
      </el-table-column>
      <el-table-column label="物流单号" align="center" prop="sendId" />
      <el-table-column label="收货地址" align="center" prop="receiveLocal" />
      <el-table-column label="用户留言" align="center" prop="userComment" />
      <el-table-column label="应付金额" align="center" prop="meet" />
      <el-table-column label="实付金额" align="center" prop="actualPayment" />
      <el-table-column label="订单状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.shop_order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发货时间" align="center" prop="deliveryTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliveryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收货时间" align="center" prop="receivingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.receivingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="modifiedBy" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          "-->
          <el-button
            v-if="scope.row.status==='2'"
            size="mini"
            type="text"
            @click="handleAddLogistics(scope.row)"
            v-hasPermi="['logistics:logistics:add']"
          >
          确认发货
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['cart:order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['cart:order:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商铺编号" prop="shopsId">
          <el-input v-model="form.shopsId" placeholder="请输入商铺编号" />
        </el-form-item>
        <el-form-item label="商品编号" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入商品编号" />
        </el-form-item>
        <el-form-item label="用户编号" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户编号" />
        </el-form-item>
        <el-form-item label="优惠券编号" prop="couponId">
          <el-input v-model="form.couponId" placeholder="请输入优惠券编号" />
        </el-form-item>
        <el-form-item label="商品数量" prop="productCount">
          <el-input v-model="form.productCount" placeholder="请输入商品数量" />
        </el-form-item>
        <el-form-item label="支付方式" prop="payType">
          <el-select v-model="form.payType" placeholder="请选择支付方式">
            <el-option
              v-for="dict in dict.type.shop_order_pay_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="配送方式" prop="sendType">
          <el-select v-model="form.sendType" placeholder="请选择配送方式">
            <el-option
              v-for="dict in dict.type.shop_order_send_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物流单号" prop="sendId">
          <el-input v-model="form.sendId" placeholder="请输入物流单号" />
        </el-form-item>
        <el-form-item label="收货地址" prop="receiveLocal">
          <el-input v-model="form.receiveLocal" placeholder="请输入收货地址" />
        </el-form-item>
        <el-form-item label="用户留言" prop="userComment">
          <el-input v-model="form.userComment" placeholder="请输入用户留言" />
        </el-form-item>
        <el-form-item label="应付金额" prop="meet">
          <el-input v-model="form.meet" placeholder="请输入应付金额" />
        </el-form-item>
        <el-form-item label="实付金额" prop="actualPayment">
          <el-input v-model="form.actualPayment" placeholder="请输入实付金额" />
        </el-form-item>
        <el-form-item label="订单状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择订单状态">
            <el-option
              v-for="dict in dict.type.shop_order_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker clearable
            v-model="form.payTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发货时间" prop="deliveryTime">
          <el-date-picker clearable
            v-model="form.deliveryTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="收货时间" prop="receivingTime">
          <el-date-picker clearable
            v-model="form.receivingTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择收货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="修改人" prop="modifiedBy">
          <el-input v-model="form.modifiedBy" placeholder="请输入修改人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加物流信息对话框 -->
    <el-dialog :title="title" :visible.sync="openLogistics" width="1100px" append-to-body>
      <el-form ref="formLogistics" :model="formLogistics" :rules="rulesLogistics" label-width="100px" inline="inline" label-position="left">
        <el-form-item label="订单ID" prop="orderId">
          <el-input v-model="formLogistics.orderId" placeholder="请输入订单ID"/>
        </el-form-item>
        <el-form-item label="物流单号" prop="logsNumber">
          <el-input v-model="formLogistics.logsNumber" placeholder="请输入物流单号"/>
        </el-form-item>
        <el-form-item label="物流公司ID" prop="companyId">
          <el-select v-model="formLogistics.companyId" clearable placeholder="请选择">
            <el-option
              v-for="item in companyList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="送件人ID" prop="postmanId">
          <el-select v-model="formLogistics.postmanId" clearable placeholder="请选择">
            <el-option
              v-for="item in postmanList"
              :key="item.userId"
              :label="item.name"
              :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物流状态" prop="status">
          <el-select v-model="formLogistics.status" placeholder="请选择物流状态">
            <el-option
              v-for="dict in dict.type.log_status_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发货地址" prop="shippingAddress">
          <el-input v-model="formLogistics.shippingAddress" placeholder="请输入发货地址"/>
        </el-form-item>
        <el-form-item label="收货地址" prop="deliveryAddress">
          <el-input v-model="formLogistics.deliveryAddress" placeholder="请输入收货地址"/>
        </el-form-item>
        <el-form-item label="详细收货地址" prop="address">
          <el-input v-model="formLogistics.address" placeholder="请输入详细收货地址"/>
        </el-form-item>
        <el-form-item label="发货时间" prop="deliveryTime">
          <el-date-picker clearable
                          v-model="formLogistics.deliveryTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm"
                          :picker-options="pickerOptions"
                          placeholder="请选择物流信息改变时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预计到达时间" prop="estimatedTime">
          <el-date-picker clearable
                          v-model="formLogistics.estimatedTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm"
                          :picker-options="pickerOptions"
                          placeholder="请选择物流信息改变时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormLogistics">确 定</el-button>
        <el-button @click="cancelLogistics">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/cart/order";
import {addLogistics, updateLogistics} from "@/api/logistics/logistics";
import {listCompany} from "@/api/logistics/company";
import {listPostman} from "@/api/logistics/postman";

export default {
  name: "Order",
  dicts: ['shop_order_send_type', 'shop_order_status', 'shop_order_pay_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      openLogistics: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productId: null,
        userId: null,
        sendId: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 物流表单参数
      formLogistics: {},

      // 表单校验
      rules: {
      },
      // 物流表单校验
      rulesLogistics: {
        orderId: [
          {required: true, message: "订单ID不能为空", trigger: "blur"}
        ],
        logsNumber: [
          {required: true, message: "物流单号不能为空", trigger: "blur"}
        ],
        companyId: [
          {required: true, message: "物流公司ID不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "物流状态不能为空", trigger: "change"}
        ],
        shippingAddress: [
          {required: true, message: "发货地址不能为空", trigger: "blur"}
        ],
        deliveryAddress: [
          {required: true, message: "收货地址不能为空", trigger: "blur"}
        ],
      },

      /*物流公司数据*/
      companyList:[],
      /*物流提交表时间组件数据*/
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', date);
          }
        }]
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listCompany().then(response => {
        this.companyList = response.rows
      })
      listPostman().then(response => {
        this.postmanList = response.rows
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 物流提交表取消按钮
    cancelLogistics() {
      this.openLogistics = false;
      this.resetLogistics();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        shopsId: null,
        productId: null,
        userId: null,
        couponId: null,
        productCount: null,
        payType: null,
        sendType: null,
        sendId: null,
        receiveLocal: null,
        userComment: null,
        meet: null,
        actualPayment: null,
        status: null,
        createTime: null,
        payTime: null,
        deliveryTime: null,
        receivingTime: null,
        modifiedBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    // 物流表单重置
    resetLogistics() {
      this.formLogistics = {
        id: null,
        orderId: null,
        logsNumber: null,
        companyId: null,
        postmanId: null,
        status: null,
        shippingAddress: null,
        deliveryAddress: null,
        address: null,
        deliveryTime: null,
        estimatedTime: null,
        actualTime: null
      };
      this.resetForm("formLogistics");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getOrder(orderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderId != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 新增按钮操作 */
    handleAddLogistics(row) {
      this.resetLogistics();
      this.formLogistics.orderId = row.orderId
      this.formLogistics.status = 1
      this.formLogistics.address = row.receiveLocal
      /*this.formLogistics.deliveryTime = row.deliveryTime*/

      this.openLogistics = true;
      this.title = "添加物流信息";
    },
    /** 物流表单提交按钮 */
    submitFormLogistics() {
      this.$refs["formLogistics"].validate(valid => {
        if (valid) {
          addLogistics(this.formLogistics).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.openLogistics = false;
            this.getList();
          });
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + orderIds + '"的数据项？').then(function() {
        return delOrder(orderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('order/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
